%%% Simulates distributions of structural parameters of interest (risk,
%%% time, consistency) based on estimated coefficient values in full_model_estimation.m

clc
clear
clear global
cd ''

global  ind_id  draw_n per_n ind_n c_char_n b_char_n m_char_n cont_char_n  thresh  factor_n X_n X_s X_ind_s theta_xy lottery_n lottery_choice_ind time_choice_n choices risk_av time_choice_ind r_xy_table step0_coeffs step2_start alpha error_mag_i error_mag_cont thetas kappas lambda_ths  rs lambda_rs scores  n_type sim_theta_i sim_kappa_i est_p_type f_n sim_factor_contribs sim_1j_contribs

load est_coeff_export



r_scale=1
%%% to get same coeffs on preference parameters below as factor_n varies
seed=1;
rng(seed);

%%%%%%%%%%%%%%%%%%%%%% 0. Factor Simulation %%%%%%%%%%%%%%%%%%%%%%
run factor_simulation


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 1. Risk Parameter Simulation %%%%%%%%%%%%%%%%%%%%%%%%

run lottery_simulation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2. Time Parameter Simulation %%%%%%%%%%%%%%%%%%%%%%%%

%%% do not run below line for robustness where risk parameters only are estimated, e.g. for
%%% expo-power utility
run time_simulation


sex=X_s(:,1);
types_tr=types';

%%% column of intercepts for each preference parameter by simulated type
int_theta=types_tr(type,1);
int_kappa=types_tr(type,2);
int_sig_th=types_tr(type,3);
int_r=types_tr(type,4);
int_sig_r=types_tr(type,5);


%%%%% Export data to stata for analysis


if K_time==1
    int_kappa_time=types_tr(type,2);
    exp_matrix=[ind_id_unique  theta_i sig_th  r_i sig_r kappa int_theta int_kappa int_sig_th int_r int_sig_r sex factors X_s type kappa_time int_kappa_time];    
else
    exp_matrix=[ind_id_unique theta_i sig_th  r_i  sig_r kappa int_theta int_kappa int_sig_th int_r int_sig_r sex factors X_s type];
end
    
% %%% replace above with below for robustness where risk parameters only 
% are estimated, e.g. for expo-power utility
% exp_matrix=[ind_id_unique theta_i sig_th kappa int_theta int_kappa int_sig_th   sex factors X_s type];


filename = 'sim_output.csv';
csvwrite(filename,exp_matrix)     